package org.grayrabbit.cms.publish.service.impl;

import java.io.ByteArrayOutputStream;
import java.util.List;

import org.grayrabbit.cloud.core.biz.BizResult;
import org.grayrabbit.cloud.core.biz.ClientUtility;
import org.grayrabbit.cloud.core.biz.ObjectArgument;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.publish.api.PublishInfo;
import org.grayrabbit.cms.publish.api.Publishable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("publishableImpl")
public class CloudPublishableImpl implements Publishable {
	private static final Logger log = LoggerFactory.getLogger(CloudPublishableImpl.class);
	public String publish(Site site, String genType, String genMode,List<String> targetIds, Admin currentAdmin, String clientIp,Boolean isAuto, Integer pageSize, Integer pageNum) {
		Object[] argments = new Object[]{site,genType,genMode,targetIds,currentAdmin,clientIp,isAuto,pageSize,pageNum};
		ObjectArgument argument = new ObjectArgument("SPRING","publishableImpl","publish",argments);
		try {
			BizResult result = (BizResult) ClientUtility.getModule("publish").submit(argument);
			if(result.getResult()!=null && result.getResult() instanceof Throwable){
				log.error("远程调用发布失败",result.getResult());
				return "1";
			}else{
				log.info("远程调用发布成功");
				return (String) result.getResult();
			}
		} catch (Exception e) {
			log.error("远程调用发布异常", e);
		} 
		return "-1";
	}

	public PublishInfo read(String publishKey, Integer offset) {
		Object[] argments = new Object[]{publishKey,offset};
		ObjectArgument argument = new ObjectArgument("SPRING","publishableImpl","read",argments);
		try {
			BizResult result = (BizResult) ClientUtility.getModule("publish").submit(argument);
			if(result.getResult()!=null && result.getResult() instanceof PublishInfo){
				return (PublishInfo) result.getResult();
			}else{
				log.error("远程查询发布信息失败",result.getResult());
			}
		} catch (Exception e) {
			log.error("远程调用发布异常", e);
		} 
		return null;
	}

	public ByteArrayOutputStream preview(Site site, String genType,List<String> targetIds, Admin currentAdmin, String clientIp)
			throws Exception {
		Object[] argments = new Object[]{site,genType,targetIds,currentAdmin,clientIp};
		ObjectArgument argument = new ObjectArgument("SPRING","publishableImpl","preview",argments);
		try {
			BizResult result = (BizResult) ClientUtility.getModule("publish").submit(argument);
			if(result.getResult()!=null && result.getResult() instanceof ByteArrayOutputStream){
				return (ByteArrayOutputStream) result.getResult();
			}else{
				log.error("远程预览调用失败",result.getResult());
			}
		} catch (Exception e) {
			log.error("远程调用发布异常", e);
		} 
		return null;
	}

	public void stop(String publishKey) {
		Object[] argments = new Object[]{publishKey};
		ObjectArgument argument = new ObjectArgument("SPRING","publishableImpl","stop",argments);
		try {
			BizResult result = (BizResult) ClientUtility.getModule("publish").submit(argument);
			if(result.getResult()!=null && result.getResult() instanceof Throwable){
				log.error("远程调用发布失败");
			}else{
				log.info("远程调用发布成功");
			}
		} catch (Exception e) {
			log.error("远程调用发布异常", e);
		} 
	}

}
